package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wd.health.R;
import com.wd.health.model.bean.RegisterBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.RsaCoder;
import com.gyf.immersionbar.ImmersionBar;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RegisterActivity extends AppCompatActivity {

    private Button btn_register;
    private EditText et_register_email;
    private EditText et_register_yanzheng;
    private EditText et_register_pwd1;
    private EditText et_register_pwd2;
    private Button tv_getemail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ImmersionBar.with(this).init();
        initView();
        initData();
    }



    private void initData() {
        tv_getemail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = et_register_email.getText().toString();
                RetrofitManager.getInstance().postEmailData(email, new Observer<RegisterBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RegisterBean registerBean) {
                        if (registerBean.getMessage().equals("发送成功")){
                            Toast.makeText(RegisterActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {

            private String pwd01;
            private String pwd02;
            @Override
            public void onClick(View v) {
                String email = et_register_email.getText().toString();
                String yanzheng = et_register_yanzheng.getText().toString();
                String pwd1 = et_register_pwd1.getText().toString();
                String pwd2 = et_register_pwd2.getText().toString();

                    try {
                        pwd01 = RsaCoder.encryptByPublicKey(pwd1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    RetrofitManager.getInstance().postRegisterData(email, yanzheng, pwd01, pwd01, new Observer<RegisterBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(RegisterBean registerBean) {
                            if (registerBean.getMessage().equals("注册成功")) {
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
//                    JMessageClient.register(email, pwd1, new BasicCallback() {
//                        @Override
//                        public void gotResult(int i, String s) {
//                            if(i==0){
//                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
//                            }
//                        }
//                    });
            }
        });
    }

    private void initView() {
        btn_register = findViewById(R.id.btn_register);
        et_register_email = findViewById(R.id.et_register_email);
        et_register_yanzheng = findViewById(R.id.et_register_yanzheng);
        et_register_pwd1 = findViewById(R.id.et_register_pwd1);
        et_register_pwd2 = findViewById(R.id.et_register_pwd2);
        tv_getemail = findViewById(R.id.tv_getemail);
    }
}